<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
<script type="text/javascript" src="../../res/js/jquery-3.1.1.min.js"></script>
<script src="../../res/layui/layui.js"></script>
<script src="../../res/js/Tools.js"></script>
<body>

<form class="layui-form" action="" lay-filter="example" id="example">
    <input type="hidden" name="schemeId" id="schemeId">
    <input type="hidden" name="athleteId" id="athleteId">
    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block">
            <input type="text" name="schemeName" id="schemename" lay-verify="required" autocomplete="off" placeholder="请输入方案名称"  class="layui-input">
        </div>
    </div>


    <div class="layui-form-item" ID="SDSD">
        <label class="layui-form-label">附件1</label>
        <button type="button" class="layui-btn layui-btn-normal" lay-verify="file1" id="test8">选择文件</button>
    </div>
    <div class="layui-form-item" ID="two">
        <label class="layui-form-label">附件2</label>
        <button type="button" class="layui-btn layui-btn-normal"  id="test9">选择文件</button>
    </div>
    <div class="layui-form-item" ID="third">
        <label class="layui-form-label">附件3</label>
        <button type="button" class="layui-btn layui-btn-normal"  id="test10">选择文件</button>
    </div>
   <!-- <div class="layui-form-item">
        <label class="layui-form-label">教练姓名</label>
        <div class="layui-input-block">
            <select name="coachId" id="coachname"  lay-verify="required">
            </select>
        </div>
    </div>-->


    <div class="layui-form-item">
        <label class="layui-form-label">运动员姓名</label>
        <div class="layui-input-block" id="checkbox">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo">立即提交</button>
        </div>
    </div>
</form>
</body>
<script>
    // $(function(){
    //
    //     select();
    //
    // });
   //下拉框
    function select(){
        /*$.ajax({
            url:"../../atsScheme/findselect",
            type:"post",
            async:false, // 全局同步
            dataType:"json",
            data:{},
            success:function(data){
                if(data !=null && data !=""){
                    layui.use(['layer','form'],function(){
                        var form = layui.form;
                        var html = '<option value="" >直接选择或搜索选择</option>';
                        for(var i=0;i<data.length;i++){;
                            html +='<option value="' +  data[i].coachId + '" id="'+data[i].coachId+'">' +   data[i].coachName + '</option>';
                        }
                        $("#coachname").html(html);
                        form.render();
                    });

                }
            }
        });*/


    }
    layui.use(['form','laydate','upload'],function (){
        var laydate = layui.laydate;
        var upload = layui.upload;
        var form = layui.form;
        var schemeId = getQueryString("schemeId");


        if(schemeId!=null){

            var url = "../../atsScheme/findById";
            var data = {schemeId:schemeId};
            $.post(url,data,function(obj){
                $("#schemeId").val(obj.schemeId);
                $("#schemename").val(obj.schemeName);
                $("#month").val(obj.month);


                $("#SDSD").append('<a href="../../'+obj.file1+'" class="layui-inline layui-upload-choose" style="margin-top:10px;padding-top:10px" target="_bulank" >下载附件1</a>');
              //  $("#two").append('<span class="layui-inline layui-upload-choose">'+obj.file2+'</span>');
              //  $("#third").append('<span class="layui-inline layui-upload-choose">'+obj.file3+'</span>');
                if(obj.file2==null){
                    $("#two").append('<span class="layui-inline layui-upload-choose" style="margin-top:0px;padding-top:6px" > </span>');
                }else{
                    $("#two").append('<a href="../../'+obj.file2+'" class="layui-inline layui-upload-choose" style="margin-top:0px;padding-top:6px"  target="_bulank">下载附件2</a>');
                }
                if(obj.file3==null){
                    $("#third").append('<span class="layui-inline layui-upload-choose" style="margin-top:0px;padding-top:6px" > </span>');
                }else{
                    $("#third").append('<a href="../../'+obj.file3+'" class="layui-inline layui-upload-choose" style="margin-top:0px;padding-top:6px" target="_bulank">下载附件3</a>');

                }
              //下拉框
               /*var url = "../../atsScheme/findselect";
                $.post(url,null,function(data){

                    $.each(data,function(index,item){
                       if(item.coachId==obj.coachId){
                           $("#"+obj.coachId).prop("selected","selected");
                           form.render("select");

                       }
                        return ;
                    });
                });*/
                var url3 = "../../atsScheme/findselects";
                $.post(url3,null,function(obj){

                    $.each(obj,function(index,item){

                          var str=0;
                          var url4 = "../../atsScheme/findCheckbox";
                          var data = {schemeId:schemeId};

                          $.post(url4,data,function (objs) {

                              for(var i=0;i<objs.length;i++){

                                  if(item.athleteId==objs[i].athleteId){

                                      str = 1;
                                      break;
                                  }

                              }
                              if(str==1){
                                  $("#checkbox").append('<input type="checkbox" class="checkbox" name="athleteName" title="'+item.athleteName+'" value="'+item.athleteId+'" checked="">');
                                  form.render("checkbox")

                              }else{

                                  $("#checkbox").append('<input type="checkbox" class="checkbox" name="athleteName" title="'+item.athleteName+'" value="'+item.athleteId+'">');
                                  form.render("checkbox")

                              }
                          },'json');
                    });
                },'json');

        });
        }else{
                //复选框
                var url2 = "../../atsScheme/findselects";
                $.post(url2,null,function(obj){
                    $.each(obj,function(index,item){
                        $("#checkbox").append('<input type="checkbox" class="checkbox" name="athleteName" title="'+item.athleteName+'" value="'+item.athleteId+'">');

                    });

                    form.render("checkbox");
                },'json');

        }
        form.on('submit(demo)',function(obj){
            var r = $("#SDSD  input").val();
            $("#two  input").attr("name","filetwo");
            $("#third  input").attr("name","filethird");
            var ids = "";
            $("input[name='athleteName']:checked").each(function(){

                ids+=$(this).val()+",";
            });
            $("#athleteId").val(ids);
            var formdata = new FormData(document.getElementById("example"));
            var url = "../../atsScheme/insert";

            if(r != null ){
                $.ajax({
                        url:url,
                        type:"post",
                        data:formdata,
                        async:false, // 全局同步
                        processData:false,
                        contentType:false,
                        dataType:"json",
                    success:function(obj){

                        if(obj.num==1){
                            layer.msg('增加成功',{
                                icon:1,
                                time:1000,
                            },function(){
                                var indexs = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(indexs);
                                parent.table2.reload();
                            });
                        }else{
                            layer.msg("增加失败");
                        }
                    },
                    error:function(e){
                        alert("失败");
                    }
                });
            }else{
                layer.msg("附件1不能为空");

            }
            return false ;
        });
        laydate.render({
            elem: '#month'
            ,type: 'month'
        });


        //选完文件后不自动上传
        upload.render({
            elem: '#test8'
            ,url: '/upload/'
            ,auto: false
            //,multiple: true
            ,accept: 'file' //普通文件
            ,bindAction: '#demo'
            ,done: function(res){
                /*if (res.num==0) {
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        window.location.href = "{:url('test/index')}"
                    }, 2000);
                }*/

            }
        });
        //选完文件后不自动上传
        upload.render({
            elem: '#test9'
            ,url: '/upload/'
            ,auto: false
            //,multiple: true
            ,accept: 'file' //普通文件
            ,bindAction: '#demo'
            ,done: function(res){
                console.log(res)
            }
        });
        //选完文件后不自动上传
        upload.render({
            elem: '#test10'
            ,url: '/upload/'
            ,auto: false
            //,multiple: true
            ,accept: 'file' //普通文件
            ,bindAction: '#demo'
            ,done: function(res){
                console.log(res)
            }
        });

        form.render();
    });



</script>
</html>